package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Blog;
import com.example.demo.mapper.BlogMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController     //定义 说明是一个返回js数据的controller
@RequestMapping("/search")     //定义接口统一路由

public class SearchController {
    @Resource          //引入mapper（不太规范）
    BlogMapper blogMapper;

    //查询数据库
    @GetMapping     //后台分页查询
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Blog> wrapper = Wrappers.<Blog>lambdaQuery();
        if (StrUtil.isNotBlank(search)){
            wrapper.like(Blog::getTitle,search);
        }
        Page<Blog> blogPage = blogMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(blogPage);
    }
}
